package com.schmix.dpp;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class CalculateNeedsActivity extends Activity{

	public static final int FAILURE = 0, SUCCESS = 1;
	public static final String TAG = "CalculateNeedsActivity";
	private SplashScreen splash;
	private Handler handler;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		splash = new SplashScreen(CalculateNeedsActivity.this);
		splash.showSplash();
		
		setContentView(R.layout.calc_needs);
		
		handler = new Handler(){
			
			public void handleMessage(Message msg){
				switch (msg.what){
				case FAILURE:
					break;
				case SUCCESS:
					Bundle b = msg.getData();
					calculateNeeds(b.getFloat("goalWeight"), b.getInt("hrsExercised"));
					break;
				default:
					Log.i(TAG, "Unexpected message code...passing to parent.");
					super.handleMessage(msg);
					break;
				}
			}
		};
		
		new PrepareActivity().execute(new Void[0]);
	}
	
	class PrepareActivity extends AsyncTask<Void, Void, Void>{

		@Override
		protected Void doInBackground(Void... params) {
			final Button submit = (Button) findViewById(R.id.calculate_needs_btn);
			submit.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					String goalWeightStr = ((EditText)findViewById(R.id.goal_weight_text)).getText().toString();
					String hrsExercisedStr = ((EditText)findViewById(R.id.exercise_hours_text)).getText().toString();
					float goalWeight;
					int hrsExercised;
					try{
						goalWeight = Float.parseFloat(goalWeightStr);
						hrsExercised = Integer.parseInt(hrsExercisedStr);
					}catch (NumberFormatException nfe){
						Log.e(TAG, "Unable to parse the values entered.");
						//show error dialog
						return;
					}
					Message msg = new Message();
					Bundle bundle = new Bundle();
					if (goalWeight > 0 && hrsExercised >= 0){
						int pos = ((Spinner)findViewById(R.id.unit_chooser)).getSelectedItemPosition();
						if (pos == 1){ //measuring in KG
							goalWeight = goalWeight * 2.2f;
						}
						bundle.putFloat("goalWeight", goalWeight);
						bundle.putInt("hrsExercised", hrsExercised);
						msg.what = SUCCESS;
					}else{
						if (goalWeight <= 0){
							//put errors
						}
						if (hrsExercised < 0){
							//put errors
						}
						msg.what = FAILURE;
					}
					msg.setData(bundle);
					handler.sendMessage(msg);
				}
			});
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			splash.removeSplash();
		}
	}
	
	private void calculateNeeds(float goalWeight, int hrsExercised){
		float calories = goalWeight * (hrsExercised <= 1 ? 10 : 9 + hrsExercised);
		float protein = goalWeight;
		float fat = (goalWeight * 0.5f);
		float carbs = (calories - ((protein * 4) + (fat * 9))) / 4;
		
		ResultsDialog rDialog = new ResultsDialog(CalculateNeedsActivity.this);
		
		((TextView)rDialog.findViewById(R.id.calories_text)).setText(Integer.toString((int)calories));
		((TextView)rDialog.findViewById(R.id.fat_text)).setText(Integer.toString((int)fat));
		((TextView)rDialog.findViewById(R.id.carbs_text)).setText(Integer.toString((int)carbs));
		((TextView)rDialog.findViewById(R.id.protein_text)).setText(Integer.toString((int)protein));
		
		rDialog.showResults();
	}
}
